package com.wholesale.commons;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.Transformation;

/**
 * 照片浏览View
 */
public class MyGrallyView extends View implements OnGestureListener {

	public static int SCREEN_WIDTH = 0;
	public static int SCREEN_HEIGHT = 0;

	private Bitmap bitmap;
	private Bitmap nBitmap;
	private Bitmap fBitmap;

	public int OffsetX = 0;
	public int OffsetY = 0;

	public static int postion = 0;
	int mLastFlingX = 0;
	boolean OffsetRight = false;

	private ArrayList<Bitmap> bitmaps;

	private GestureDetector myGesture;

	public MyGrallyView(Context context, AttributeSet attrs) {
		super(context, attrs);
		myGesture = new GestureDetector(context, this);
		setLongClickable(true);
	}

	public MyGrallyView(Context context) {
		super(context);
		myGesture = new GestureDetector(context, this);
		setLongClickable(true);
	}
	public MyGrallyView(Context context,ArrayList<Bitmap> bitmaps) {
		super(context);
		this.bitmaps = bitmaps;
		bitmap = getBitmap(0);
		nBitmap = getBitmap(1);
		myGesture = new GestureDetector(context, this);
		setLongClickable(true);
	}
	@Override
	public void draw(Canvas canvas) {
		// System.out.println("bitmaps size:"+bitmaps.length+" ,SCREEN_WIDTH "+SCREEN_WIDTH
		// + "   SCREEN_HEIGHT:"+SCREEN_HEIGHT);
//		if (SCREEN_WIDTH == 0) {
			SCREEN_WIDTH = getWidth();
			int SCREEN_HEIGHT = getHeight() - 20;
//		}
		Paint p = new Paint();
		canvas.drawColor(Color.argb(0, 255, 0, 0));
		if (OffsetX < 0) {
			if (nBitmap != null) {
				Rect rectTemp = new Rect(SCREEN_WIDTH + 15 + OffsetX, 0,
						SCREEN_WIDTH + 15 + OffsetX + SCREEN_WIDTH,
						SCREEN_HEIGHT);
				canvas.drawBitmap(nBitmap, null, rectTemp, p);
			}
		} else if (OffsetX > 0) {
			if (fBitmap != null) {
				Rect rectTemp = new Rect(-SCREEN_WIDTH - 15 + OffsetX, 0,
						-SCREEN_WIDTH - 15 + OffsetX + SCREEN_WIDTH,
						SCREEN_HEIGHT);
				canvas.drawBitmap(fBitmap, null, rectTemp, p);
			}
		}
		if (bitmap != null) {
			Rect rectTemp = new Rect(OffsetX, OffsetY, OffsetX + SCREEN_WIDTH,
					OffsetY + SCREEN_HEIGHT);
			canvas.drawBitmap(bitmap, null, rectTemp, p);
		} 
		if (bitmaps != null) {
			if (bitmaps.size() > 0) {
				int w = this.getWidth();
				w = (w - bitmaps.size() * 15) / 2;
				for (int i = 0; i < bitmaps.size(); i++) {
					if (i == postion) {
						p.setColor(Color.YELLOW);
					} else {
						p.setColor(Color.WHITE);
					}
					// 黄色区域
					canvas.drawCircle(w + 15 * i, this.getHeight() - 10, 5, p);
				}

			}
		}
	}

	public void setBitmaps(ArrayList<Bitmap> bitmaps) {
		this.bitmaps = bitmaps;
		bitmap = getBitmap(0);
		nBitmap = getBitmap(1);
	}

	public int getPostion(){
		return postion ;
	}
	
	public void setOnDoubleTapListener(OnDoubleTapListener doubleTapListener) {
		myGesture.setOnDoubleTapListener(doubleTapListener);
	}

	public void handleScroll(int deltaX) {
		if (deltaX > 0) {
			OffsetX -= -deltaX;
		} else {
			OffsetX += deltaX;
		}
		invalidate();
	}

	// 标记为可以切换到下一张
	boolean flag = false;
	// 标记为需要向右滑动
	boolean flag1 = false;
	// 标记为需要向左滑动
	boolean flag2 = false;

	class MyAnimation extends Animation {

		private int temp;

		@Override
		public void initialize(int width, int height, int parentWidth,
				int parentHeight) {
			temp = OffsetX;
			super.initialize(width, height, parentWidth, parentHeight);
			setDuration(500);
			setFillAfter(true);
			setInterpolator(new LinearInterpolator());
		}

		@Override
		protected void applyTransformation(float interpolatedTime,
				Transformation t) {
			// Log.i("bb", "OffsetX==>"+OffsetX);
			// 需要滑动图片时根据方向来变换OffsetX大小
			if (flag) {
				if (temp > 0) {
					OffsetX = (int) ((SCREEN_WIDTH - temp) * interpolatedTime + temp);
				} else {
					OffsetX = (int) ((-SCREEN_WIDTH - temp) * interpolatedTime + temp);
				}
				// 不需要变换的情况
			} else {
				OffsetX = (int) (temp * (1 - interpolatedTime));
			}

			invalidate();
		}
	}

	// 动画结束后需要做一些工作
	@Override
	protected void onAnimationEnd() {
		if (flag1) {
			nBitmap = bitmap;
			bitmap = fBitmap;
			fBitmap = null;
			postion = postion - 1;
		} else if (flag2) {
			fBitmap = bitmap;
			bitmap = nBitmap;
			nBitmap = null;
			postion = postion + 1;
		}
		flag1 = false;
		flag2 = false;
		OffsetX = 0;
		if (fBitmap == null && OffsetX == 0) {
			if (postion > 0) {
				fBitmap = getBitmap(postion - 1);
			}

		} else if (nBitmap == null && OffsetX == 0) {
			if (postion < bitmaps.size() - 1) {
				nBitmap = getBitmap(postion + 1);
			}
		}
		clearAnimation();
		flag = false;
	}

	public void setPostion(int postion) {
		this.postion = postion;
		bitmap = getBitmap(postion);
		nBitmap = getBitmap(postion + 1);
		invalidate();
	}

	public void onFling(int paramFloat1) {
		if (OffsetX > SCREEN_WIDTH / 3) {
			if (fBitmap != null) {
				flag = true;
				flag1 = true;
			}
		} else if (OffsetX < -SCREEN_WIDTH / 3) {
			if (nBitmap != null) {
				flag = true;
				flag2 = true;
			}
		}
		// 开始动画效果
		startAnimation(new MyAnimation());
		invalidate();

	}

	/**
	 * 获得当前位置的图片
	 * 
	 * @param currentPos
	 * @return
	 */
	public Bitmap getBitmap(int currentPos) {
		if (currentPos > bitmaps.size()- 1) {
			return null;
		}
		Bitmap currBitmap = bitmaps.get(currentPos);
		OffsetX = 0;
		OffsetY = 0;

		return currBitmap;
	}

	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	// 手势完成后调用
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		onFling((int) -velocityX);
		return true;
	}

	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	// 滑动过程一直在调用
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		handleScroll(-1 * (int) distanceX);
		return true;
	}

	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean setOnTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_UP:
			onFling(0);
			break;
		}

		return myGesture.onTouchEvent(event);
	}
}